package ClientFront;

import ClientFront.Net.NetMsgDealer;

import javax.swing.*;
import java.awt.*;

public class MainWindow extends JFrame implements Panelswitch {
    //设置主窗口大小
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    //设置标题
    public static final String TITLE = "五子棋";
    //卡片布局
    private CardLayout cardLayout = new CardLayout();				//多界面布局

    private JPanel mainPanel = new JPanel();						//总窗口panel
    private JPanel contentPane = new JPanel();							//主界面
    private ClientLoginPanel loginPanel = new ClientLoginPanel();       //登录界面
    private ClientGamePanel gamePanel = new ClientGamePanel();
    public static final String loginPanelName = "登录界面";
    public static final String gamePanelName = "游戏界面";

    public NetMsgDealer msgDealer = new NetMsgDealer();

    public static final int LOGIN_PANEL_WIDTH = 300;
    public static final int LOGIN_PANEL_HEIGHT = 350;

    public MainWindow(){
        setResizable(false);	//不可放大缩小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭退出
        setTitle(TITLE);//标题

        setSize(WIDTH,HEIGHT);//大小
        setLocationRelativeTo(null);//窗口居中
        contentPane.setSize(WIDTH, HEIGHT);//设置主界面大小
        contentPane.setLayout(null);//用bound定位

        mainPanel.setLayout(cardLayout);//设为多界面布局，最上一张可见
        mainPanel.add(loginPanel,loginPanelName);
        mainPanel.add(gamePanel,gamePanelName);
        setContentPane(mainPanel);
        loginPanel.setParentWindowSwitch(this);
        switchPanelTo(loginPanelName);
        //切换为登录界面，把消息收发器给登录界面初始化



    }
    public void switchPanelTo(String panelName){
        switch (panelName){
            case loginPanelName:
                loginPanel.setMsgDealer(msgDealer);
                msgDealer.setMsgReciver(loginPanel);
                setTitle(loginPanelName);
                setSize(LOGIN_PANEL_WIDTH,LOGIN_PANEL_HEIGHT);
                cardLayout.show(mainPanel,loginPanelName);
                break;
            case gamePanelName:
                gamePanel.setMsgDealer(msgDealer);
                msgDealer.setMsgReciver(gamePanel);//设置消息返回对象
                setTitle(gamePanelName);
                setSize(1200,900);
                gamePanel.initWithOption(loginPanel.getUserName());
                cardLayout.show(mainPanel,gamePanelName);
                break;
        }
    }
}
